package edu.buu.refectory.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.buu.refectory.constant.Const;
import edu.buu.refectory.constant.EM;
import edu.buu.refectory.domain.Refectory;
import edu.buu.refectory.domain.User;
import edu.buu.refectory.service.RefectoryService;
import edu.buu.refectory.service.UserService;
import edu.buu.refectory.utils.CommonUtil;
import edu.buu.refectory.utils.MD5Util;

@Controller
public class LoginController {
    
    private static final Logger log = Logger.getLogger(LoginController.class);
    @Autowired
    private UserService userService;
    
    @Autowired
    private RefectoryService refectoryService;
    
    
    @RequestMapping(value="/logout")
    @ResponseBody
    public  Map<String, Object> logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("auth_user");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put(Const.SUCCESS, false);
        return resultMap;
    }
    @RequestMapping(value="/login")
    public String login(HttpServletRequest request){
        return "/loginUI";
    }

    @RequestMapping(value="/user/signup",method = RequestMethod.POST)
    @ResponseBody
    public Object signup(HttpServletRequest request,HttpServletResponse response){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String username = request.getParameter("uName");
        String password = request.getParameter("uKey");
        String encryptedPassword = MD5Util.encrypt(password, username);
        
        List <User> userList = userService.getByUsername(username);
        User user = null;
        if(userList!=null && userList.size() > 0){
            user = userList.get(0);
            if(encryptedPassword.equals(user.getuKey())){//本系统登录通过
            	HttpSession session = request.getSession();
                session.setAttribute("auth_user", user);
                resultMap.put("user", user);
                resultMap.put("SUCCESS", true);
                log.info(user.getuName()+":user verify success!");
                return resultMap;              
            }else{//本系登录统验证不通过
                log.info(user.getuName()+":user verify failure!");
                resultMap.put(Const.ERROR_MESSAGE, EM.USER_PASSWORD_ERROR);
        		resultMap.put(Const.SUCCESS, false);   
        		return resultMap;
            }  
        }
        //用户不存在
        resultMap.put(Const.ERROR_MESSAGE, EM.USER_NAME_ERROR);
        resultMap.put(Const.SUCCESS, false);   
        return resultMap;
    }
    @RequestMapping(value="/refectory/signup",method = RequestMethod.POST)
    @ResponseBody
    public Object signupRe(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	String username = request.getParameter("rUsername");
    	String password = request.getParameter("rKey");
    	String encryptedPassword = MD5Util.encrypt(password, username);
    	
    	List <Refectory> refectoryList = refectoryService.getByUsername(username);
    	Refectory refectory = null;
    	if(refectoryList!=null && refectoryList.size() > 0){
    		refectory = refectoryList.get(0);
    		if(encryptedPassword.equals(refectory.getrKey())){//本系统登录通过
    			HttpSession session = request.getSession();
    			session.setAttribute("auth_refectory", refectory);
    			resultMap.put("refectory", refectory);
    			resultMap.put("SUCCESS", true);
    			log.info(refectory.getrName()+":user verify success!");
    			return resultMap;              
    		}else{//本系登录统验证不通过
    			log.info(refectory.getrName()+":user verify failure!");
    			resultMap.put(Const.ERROR_MESSAGE, EM.USER_PASSWORD_ERROR);
    			resultMap.put(Const.SUCCESS, false);   
    			return resultMap;
    		}  
    	}
    	//用户不存在
    	resultMap.put(Const.ERROR_MESSAGE, EM.USER_NAME_ERROR);
    	resultMap.put(Const.SUCCESS, false);   
    	return resultMap;
    }
    
    @RequestMapping(value="/getCurrentUser")
    @ResponseBody
    public  User getCurrentUser(HttpServletRequest request) {
        User user = null;
        try{
            user =  CommonUtil.getCurrentUser(request);
        }catch (Exception e) {
            log.error("getCurrentUser",e);
        }
        return user;
    }
}
